#include<iostream>
#include<unistd.h>

using namespace std;

//模拟孤儿进程（父进程在子进程前退出）
int main()
{
    pid_t pid=fork();
    if(pid<0)
    {
        cerr<<"Fork failed"<<endl;
        return 1;
    }
    else if(pid==0)       
    {
        // Child process
        cout<<"Child process with PID: "<<getpid()<<endl;
        sleep(20); // Simulate some work
        cout<<"Child process exiting"<<endl;
        return 0;
    }
    else if(pid>0)
    {
        // Parent process
        cout<<"Father process with PID: "<<getpid()<<endl;
        sleep(5);
        return 0;     //父进程退出
    }
    return 0;
}